package com.bocai.kelalacar.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.bocai.kelalacar.R;
import com.bocai.kelalacar.bean.ApplyWayBean;
import com.bocai.kelalacar.ui.car.set.ada.WithdrawAda;

import java.util.List;


/**
 * Created by yuanfei on 2017/6/23.
 */

public class WithdrawPopupWindow extends PopupWindow  {

    ImageView imgHide;
    private View menuView;
    private Activity mContext;
    LayoutInflater inflater;
    ListView listView;
    WithdrawAda ada;
    private ApplyWayBean bean;
    private WithdrawPopupInterface withdrawPopupInterface;


    public WithdrawPopupWindow(Activity mContext, final ApplyWayBean bean, final WithdrawPopupInterface withdrawPopupInterface) {
        this.mContext = mContext;
        this.bean = bean;
        this.withdrawPopupInterface = withdrawPopupInterface;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        menuView = inflater.inflate(R.layout.popupwindow_withdraw, null);
        imgHide = (ImageView) menuView.findViewById(R.id.img_hide);
        listView = (ListView) menuView.findViewById(R.id.list);
        ada = new WithdrawAda(mContext,bean);
        listView.setAdapter(ada);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (bean.getBankCard().size()>position){
                    String bb = bean.getBankCard().get(position).getAccount().substring(bean.getBankCard().get(position).getAccount().length()-4,bean.getBankCard().get(position).getAccount().length());
//            viewHolder.tvAccount.setText("**** **** **** "+bb);
                    withdrawPopupInterface.getInformation("",bb,bean.getBankCard().get(position).getWayId());
                }else if (bean.getBankCard().size() + 1>position){
                    if (bean.getAlipay()!=null&&!TextUtils.isEmpty(bean.getAlipay().getWayId())){
                        withdrawPopupInterface.getInformation("支付宝",bean.getAlipay().getAccount(),bean.getAlipay().getWayId());
                    }else {
                        withdrawPopupInterface.getInformation("微信",bean.getWeixin().getAccount(),bean.getWeixin().getWayId());
                    }
                }else {
                    withdrawPopupInterface.getInformation("微信",bean.getWeixin().getAccount(),bean.getWeixin().getWayId());
                }
            }
        });

        imgHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //取消按钮
        imgHide.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                backgroundAlpha(1f);
                dismiss();
            }
        });
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(menuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        menuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = menuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        backgroundAlpha(1f);
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    public boolean getBean(){
        if (bean!=null){
            if (bean.getAlipay()!=null||bean.getWeixin()!=null&&bean.getBankCard().size()!=0){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }

    }


    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mContext.getWindow().setAttributes(lp);
    }


}
